package com.retail.member.client.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.result.Result;
import com.retail.member.client.OrderClient;
import com.retail.member.entity.request.OrderStasticListRequest;
import com.retail.member.entity.vo.RetailOrderResVo;
import com.retail.member.entity.vo.RetailOrderStasticResVo;
import com.retail.order.entity.vo.RetailSettleOrderVo;
import com.retail.order.service.RetailSettleOrderService;
import com.retail.shop.entity.vo.RetailShopVo;
import com.retail.shop.enums.ShopSourceEnums;
import com.retail.shop.service.RetailShopService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.enums.UserTypeEnums;
import com.retail.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("orderClient")
public class OrderClientImpl implements OrderClient {
    @Autowired
    private RetailSettleOrderService retailSettleOrderService;

    @Autowired
    private RetailShopService retailShopService;

    @Autowired
    private AdminService adminService;


    @Override
    public Result<List<RetailSettleOrderVo>> findOwnerOrderByAdminId(String adminId) {
        List<RetailSettleOrderVo> orderVos = retailSettleOrderService.findOwnerOrderByAdminId(adminId);
        return Result.success(orderVos);
    }

    @Override
    public Result<List<RetailSettleOrderVo>> myOrderList(OrderStasticListRequest request) {
        AdminVo adminVo = adminService.getModel(request.getAdminId());
        List<RetailSettleOrderVo> retus = new ArrayList<>();
        List<RetailSettleOrderVo> orderVos = null;
        if (ObjectUtil.equal(adminVo.getUserType(), UserTypeEnums.BUSINESS_TYPE.getValue())) {
           orderVos = retailSettleOrderService.myOrderList(request.getAdminId() + "");
        } else {
            orderVos = retailSettleOrderService.myShopOrderList(request.getAdminId() + "");
        }

        for (RetailSettleOrderVo data : orderVos) {
            if (ObjectUtil.isEmpty(request.getSearch())) {
                retus.add(data);
            } else {
                if (ObjectUtil.contains(data.getProductName(), request.getSearch())) {
                    retus.add(data);
                }
            }
        }
        return Result.success(retus);
    }

    @Override
    public Result<List<RetailOrderStasticResVo>> findOwnerOrderStastic(Long adminId) {
        List<RetailSettleOrderVo> datas = findOwnerOrderList(adminId);
        RetailOrderStasticResVo initOrder = RetailOrderStasticResVo.builder().orderKey(0).orderCode("OrderStatus_Init").orderLabel("初始化").orderCount(0).build();
        RetailOrderStasticResVo waitPayOrder = RetailOrderStasticResVo.builder().orderKey(100).orderCode("OrderStatus_WaitPay").orderLabel("待支付").orderCount(0).build();
        RetailOrderStasticResVo cancelOrder = RetailOrderStasticResVo.builder().orderKey(101).orderCode("OrderStatus_Cancel").orderLabel("订单关闭").orderCount(0).build();
        RetailOrderStasticResVo paySuccessOrder = RetailOrderStasticResVo.builder().orderKey(200).orderCode("OrderStatus_PaySuccess").orderLabel("支付成功").orderCount(0).build();
        RetailOrderStasticResVo availableOrder = RetailOrderStasticResVo.builder().orderKey(201).orderCode("OrderStatus_Available").orderLabel("待使用").orderCount(0).build();
        RetailOrderStasticResVo finishOrder = RetailOrderStasticResVo.builder().orderKey(1).orderCode("OrderStatus_Finish").orderLabel("交易成功").orderCount(0).build();
        for (RetailSettleOrderVo data : datas) {
            if (ObjectUtil.equal(data.getOrderStatus(), 0)) {
                initOrder.setOrderCount(initOrder.getOrderCount() + 1);
            } else if (ObjectUtil.equal(data.getOrderStatus(), 100)) {
                waitPayOrder.setOrderCount(waitPayOrder.getOrderCount() + 1);
            } else if (ObjectUtil.equal(data.getOrderStatus(), 101)) {
                cancelOrder.setOrderCount(cancelOrder.getOrderCount() + 1);
            } else if (ObjectUtil.equal(data.getOrderStatus(), 200)) {
                paySuccessOrder.setOrderCount(paySuccessOrder.getOrderCount() + 1);
            } else if (ObjectUtil.equal(data.getOrderStatus(), 201)) {
                availableOrder.setOrderCount(availableOrder.getOrderCount() + 1);
            } else if (ObjectUtil.equal(data.getOrderStatus(), 1)) {
                finishOrder.setOrderCount(finishOrder.getOrderCount() + 1);
            }
        }

        List<RetailOrderStasticResVo> retus = new ArrayList<>();
        retus.add(initOrder);
        retus.add(waitPayOrder);
        retus.add(cancelOrder);
        retus.add(paySuccessOrder);
        retus.add(availableOrder);
        retus.add(finishOrder);

        CollectionUtil.sort(retus, (o1, o2) -> o1.getOrderCount() > o2.getOrderCount() ? -1 : 1);

        return Result.success(retus);

    }

    @Override
    public Result<List<RetailSettleOrderVo>> findOwnerOrderStasticList(Long adminId, Integer orderType, String search) {
        List<RetailSettleOrderVo> datas = findOwnerOrderList(adminId);
        List<RetailSettleOrderVo> retus = new ArrayList<>();
        for (RetailSettleOrderVo data : datas) {
            if (ObjectUtil.equal(data.getOrderStatus(), orderType)) {
                if (ObjectUtil.isEmpty(search)) {
                    retus.add(data);
                } else {
                    if (ObjectUtil.contains(data.getProductName(), search)) {
                        retus.add(data);
                    }
                }
            }
        }

        return Result.success(retus);
    }

    @Override
    public Result<RetailOrderResVo> orderDetail(Long orderId) {
        RetailOrderResVo resVo = new RetailOrderResVo();
        RetailSettleOrderVo retailSettleOrderVo = retailSettleOrderService.getModel(orderId);
        retailSettleOrderVo.setOrderSourceName(ShopSourceEnums.getStatusValues(retailSettleOrderVo.getOrderSource()));
        resVo.setOrder(retailSettleOrderVo);

        if (ObjectUtil.isNotEmpty(retailSettleOrderVo.getPoiId())) {
            RetailShopVo shopVo = retailShopService.getModelByPoiIdLimmit1(retailSettleOrderVo.getPoiId());
            if (ObjectUtil.isNotEmpty(shopVo)) {
                resVo.setShop(shopVo);
            } else {
                resVo.setShop(new RetailShopVo());
            }

            AdminVo adminVo = adminService.getModelByPoiId(retailSettleOrderVo.getPoiId());
            if (ObjectUtil.isNotEmpty(adminVo)) {
                resVo.setAdmin(adminVo);
            } else {
                resVo.setAdmin(new AdminVo());
            }
        } else {
            resVo.setShop(new RetailShopVo());
            resVo.setAdmin(new AdminVo());
        }

        return Result.success(resVo);
    }

    public List<RetailSettleOrderVo> findOwnerOrderList(Long adminId) {
        List<RetailSettleOrderVo> orders = retailSettleOrderService.findOwnerOrderByAdminIdNotInVerifDetail(adminId);
        orders.forEach(order -> {
            order.setOrderLevel(1);
        });
        List<AdminVo> childAdmins = adminService.childAdminByParentId(adminId);

        List<RetailSettleOrderVo> allResults = new ArrayList<>();
        allResults.addAll(orders);

        for (AdminVo admin : childAdmins) {
            List<RetailSettleOrderVo> order1 = retailSettleOrderService.findOwnerOrderByAdminIdNotInVerifDetail(admin.getId());
            order1.forEach(order -> {
                order.setOrderLevel(2);
            });
            allResults.addAll(order1);

            List<AdminVo> childAdmins1 = adminService.childAdminByParentId(admin.getId());

            for (AdminVo admin2 : childAdmins1) {
                List<RetailSettleOrderVo> order2 = retailSettleOrderService.findOwnerOrderByAdminIdNotInVerifDetail(admin2.getId());
                order2.forEach(order -> {
                    order.setOrderLevel(3);
                });
                allResults.addAll(order2);
            }
        }

        return allResults;
    }
}
